/**
 * Created by ying on 16-7-27.
 */
$(document).ready(function () {
    var sum_count = get_sum_count()
    show_sum_count(sum_count)
    var goods_information = update_promotion_information()
    generate_goods_info(goods_information);
    var sum_money = compute_sum_money()
    show_sum_money(sum_money)
    add_click_event()
    jump_page()
    add_input_count_event()
});
function get_sum_count() {
    var sum_count = get_local("sum_count") || 0;
    return sum_count
}
function get_local(key){
    return JSON.parse(localStorage.getItem(key))
}
function set_local(key){
    return localStorage.setItem(key,JSON.stringify(key))
}
function show_sum_count(sum_count) {
    $("#sum_count").html(sum_count);
}
function save_local_data(goods_information) {
    localStorage.setItem("data",JSON.stringify(goods_information))
}
function update_count( change_num,goods_info,good_index) {
        goods_info[good_index].count+=change_num
    if (sum_count == 0) {
        window.location.href = "../html/product_list.html"
    }
}
function show_goods_count( goods_name) {
    var good_info = get_good_info_by_name(goods_name)
    var count=good_info.count
    $('#'+goods_name+' input').val(count);
}
function compute_subtotal(goods_info) {
    return _.each(goods_info, function (info) {
        if (info.pro_count != 0) {
            info.subtotal =info.price * (info.count - info.pro_count)+ "元（原价： " + info.price * info.count + "元）";
        } else {
            info.subtotal = info.price * (info.count - info.pro_count)+"元";
        }
    })
}
function compute_sum_count(change_num) {
    var sum = get_local("sum_count")
    sum += change_num
    localStorage.setItem("sum_count", sum)
}
function judgment_goods_count(goods_info) {
    var delete_info=_.remove(goods_info, function (info) {
        return info.count == 0;
    })
    update_name_arr(goods_info);
    save_local_data(goods_info);
}
function update_name_arr(goods_info){
    var replace_name=[]
    _.each(goods_info,function(info){
        replace_name.push(info.name);
    })
    localStorage.setItem("name",JSON.stringify(replace_name));
}
function get_good_index(goods_name) {
    return get_local("name").indexOf(goods_name);
}
function update_goods_info(goods_name, change_num) {
    var goods_info = get_local("data");
    var good_index = get_good_index(goods_name);
    update_count(change_num,goods_info,good_index);
    get_pro_count(goods_info);
    judgment_goods_count(goods_info);
    compute_subtotal(goods_info);
    compute_sum_count(change_num);
    save_local_data(goods_info);
    return goods_info;
}
function add_click_event() {
    $("td button").click(function () {
        var goods_info = get_local("data")
        var goods_name = $(this).parents("tr").find(".name").html();
        var change_num = this.className.match('add') ? 1 : -1;
        goods_info = update_goods_info(goods_name, change_num);
        change_good_show(goods_name,goods_info);
        show_sum_money_and_count();
    })
}
function show_sum_money_and_count(){
    var sum_money = compute_sum_money();
    show_sum_money(sum_money);
    var sum_count = get_sum_count();
    show_sum_count(sum_count);
}
function add_input_count_event(){
    $("input").on('input',function(){
        var goods_info = get_local("data")
        var goods_name = $(this).parents("tr").find(".name").html();
        var input=$(this);
        input_update_local(goods_info,goods_name,input);
        var good_info = get_good_info_by_name(goods_name);
        show_subtotal(goods_name,good_info);
        show_sum_money_and_count();
    })
}
function input_update_local(goods_info,goods_name,input){
    var start_count;
    var input_count=input.val();
    _.each(goods_info,function(info){
        if(goods_name==info.name){
            start_count=info.count;
            info.count=Number(input_count);
        }
    })
    var local_sum_count=Number(get_local("sum_count"))
    var input_num=Number(input_count)-Number(start_count)+local_sum_count;
    localStorage.setItem("sum_count",input_num);
    get_pro_count(goods_info);
    compute_subtotal(goods_info);
    save_local_data(goods_info);
}
function change_good_show(goods_name,goods_info) {
    var good_index = get_good_index(goods_name);
    var good_info = get_good_info_by_name(goods_name)
    if (good_index == -1) {
        $('#' + goods_name).remove();
    }else{
        show_goods_count(goods_name)
        show_subtotal(goods_name,good_info)
    }
}
function show_subtotal(goods_name,good_info){
    var subtotal=good_info.subtotal;
    $('#'+ goods_name +' .pro').html(subtotal)
}
function get_good_info_by_name(goods_name) {
    var good_index = get_good_index(goods_name);
    var goods_info = get_local("data")
    return goods_info[good_index]
}
function get_pro_count(goods_information) {
    var free_goods_name = ["可口可乐", "方便面", "雪碧"]
    return _.each(goods_information,function(info){
        if (free_goods_name.indexOf(info.name) != -1) {
            info.pro_count = Math.floor(info.count/3)
        }else{info.pro_count=""}
    })
}
function compute_sum_money() {
    var goods_information =get_local("data")
    var sum_money = 0;
    _.each(goods_information, function (info) {
        sum_money += info.price * (info.count - info.pro_count);
    })
    return sum_money
}
function show_sum_money(sum_money) {
    $("#sum_money").html(sum_money.toFixed(2));
}
function update_promotion_information() {
    var goods_information =get_local("data")
    goods_information = get_pro_count(goods_information)
    compute_subtotal(goods_information)
    save_local_data(goods_information);
    return goods_information
}
function generate_goods_info(goods_information) {
    $("#form").tmpl(goods_information).appendTo("#product_list");
}
function jump_page() {
    $(".pay").click(function () {
        window.location.href = "../html/pay.html"
    })
}